/*
 * pps_bom
 */
package com.pitop.material.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baidu.fsg.uid.UidGenerator;
import com.pitop.core.base.BaseDAO;
import com.pitop.core.base.BaseSVImpl;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.exceptions.MaterialLogException;
import com.pitop.material.dao.MaterialLogDAO;
import com.pitop.material.entity.MaterialLog;
import com.pitop.material.entity.MaterialLogState;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("materialLogSV")
@Slf4j
public class MaterialLogSVImpl extends BaseSVImpl<MaterialLog, Long> implements MaterialLogSV {

    @Autowired
    private MaterialLogDAO materialLogDAO;

    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return materialLogDAO;
    }

    /**
    * 保存account对象
    *
    * @param entity 实体
    * @throws BaseException
    */
    @Override
    public void save(MaterialLog entity) throws BaseException {
        entity.setCreateTime(new Date());
        super.save(entity);
    }

    /**
     * 加载一个对象MaterialLog
     * @param id 
     * @return MaterialLog
     */
    @Override
    public MaterialLog load(Long id) {
       if(id==null){
             throw new MaterialLogException(BaseException.BaseExceptionEnum.Illegal_Param);
       }

       Map<String,Object> param=new HashMap<>();
       param.put("id",id);
       return materialLogDAO.load(param);
    }


    /**
     * 加载一个对象MaterialLog 通过id
     * @param id 
     * @return MaterialLog
     */
    @Override
    public MaterialLog loadById(Long id) {
       if(id==null){
           throw new MaterialLogException(BaseException.BaseExceptionEnum.Illegal_Param);
       }
       return materialLogDAO.loadById(id);
    }


    /**
    * 根据主键id,oldStates 共同更新 MaterialLog 的状态到newState状态
    *
    * @param id 
    * @param newState 新状态
    * @param oldStates 旧状态集合
    */
    @Override
    public void updateStateById(Long id,MaterialLogState newState,MaterialLogState... oldStates){
        if(id==null){
            throw new MaterialLogException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if(newState==null){
            throw new MaterialLogException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if(oldStates==null){
            throw new MaterialLogException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        materialLogDAO.updateStateById(id,new Date(),newState.name(), states);
    }

    /**
    * 根据主键id 更新 MaterialLog 的状态到另一个状态
    *
    * @param id 
    * @param state 状态
    */
    @Override
    public void updateById(Long id,MaterialLogState state){
        if(id==null){
           throw new MaterialLogException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        materialLogDAO.updateById(id,state.name(),new Date());
    }


    /**
     * 删除对象MaterialLog
     * @param id 
     * @return MaterialLog
     */
    @Override
    public void delete(Long id) {
       if(id==null){
           throw new MaterialLogException(BaseException.BaseExceptionEnum.Illegal_Param);
       }
       Map<String,Object> param=new HashMap<>();
       param.put("id",id);
       materialLogDAO.delete(param);
    }



    /**
    * 查询MaterialLog分页
    *
    * @param materialLog  对象
    * @param offset 查询开始行
    * @param limit  查询行数
    * @return List<MaterialLog>
    */
    @Override
    public List<MaterialLog> list(MaterialLog materialLog, int offset, int limit) {
        if (offset < 0) {
           offset = 0;
        }

        if (limit < 0) {
           limit = Page.limit;
        }

        Map<String, Object> map = null;
        if (materialLog != null) {
           map = JSON.parseObject(JSON.toJSONString(materialLog, SerializerFeature.WriteDateUseDateFormat));
        } else {
           map = new HashMap<>();
        }
        return materialLogDAO.list(map, new RowBounds(offset, limit));
    }

    @Override
    public  int count(MaterialLog materialLog){
        Map<String, Object> map = null;
        if (materialLog != null) {
           map = JSON.parseObject(JSON.toJSONString(materialLog, SerializerFeature.WriteDateUseDateFormat));
        } else {
           map = new HashMap<>();
        }
       return materialLogDAO.count(map);
    }

    /**
     * 查询MaterialLog分页
     *
     * @param id  
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<MaterialLog>
     */
    @Override
    public List<MaterialLog> list(Long id,int offset, int limit) {
        if (offset < 0) {
          offset = 0;
        }

        if (limit < 0) {
          limit = Page.limit;
        }
        Map<String,Object> param=new HashMap<>();
        param.put("id",id);
        return materialLogDAO.list(param,new RowBounds(offset, limit));
    }

    @Override
    public int count(Long id) {
        Map<String,Object> param=new HashMap<>();
        param.put("id",id);
        return materialLogDAO.count(param);
    }

}
